<?php
# AES
namespace app\server;

class Aes extends Server
{
    const METHOD = 'AES-128-CBC';
    const KEY = 'aes-easyapi-62300.!@#';

    // 加密
    static public function encode($data)
    {
        $method = self::METHOD;
        $system = parent::getSystem();
        $key = isset($system['aesKey']) ? $system['aesKey'] : self::KEY;
        $key = substr(sha1($key), 7, openssl_cipher_iv_length($method));
        $iv = $key;
        return openssl_encrypt($data, $method, $key, 0, $iv);
    }

    // 解密
    static public function decode($data)
    {
        $method = self::METHOD;
        $system = parent::getSystem();
        $key = isset($system['aesKey']) ? $system['aesKey'] : self::KEY;
        $key = substr(sha1($key), 7, openssl_cipher_iv_length($method));
        $iv = $key;
        return openssl_decrypt($data, $method, $key, 0, $iv);
    }
}